package com.pawernode.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class MessageService {

    // 这就是模板方法设计模式
    @Resource
    private RabbitTemplate rabbitTemplate;



    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(
                // 匿名内部类
                new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                log.error("消息从交换机没有正确的路由到（投递到）队列，原因为：{}",returnedMessage.getReplyText());
            }
        }); // 设置回调
    }

    public void sendMsg() {
        Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
                .build();
        rabbitTemplate.convertAndSend("exchange.return.3", "info", message);
        log.info("消息发送完毕，发送时间为：{}", new Date());
    }


}
